//
//  ViewController.m
//  scrollView子控件悬停及头部下拉放大功能
//
//  Created by zxy on 2018/10/18.
//  Copyright © 2018年 zxy. All rights reserved.
//

#import "ViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ( [UIScreen mainScreen].bounds.size.height)
CGFloat const imgH = 160;
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrolV;
@property(nonatomic,strong)UIImageView *headerImageView;
@property(nonatomic,strong)UIView *toolBar;
@property(nonatomic,strong)UIView *blueView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubView];
}

-(void)setupSubView{
    if (@available(iOS 11, *)) {
        self.scrolV.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.scrolV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.scrolV.delegate =self;
    [self.view addSubview:self.scrolV];
    
    self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, imgH)];
    self.headerImageView.image = [UIImage imageNamed:@"1.jpg"];
    [self.scrolV addSubview:self.headerImageView];
    
    self.toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerImageView.frame),SCREEN_WIDTH, 40)];
    self.toolBar.backgroundColor = [UIColor orangeColor];
    [self.scrolV addSubview:self.toolBar];
    
    self.blueView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.toolBar.frame), SCREEN_WIDTH,700)];
    self.blueView.backgroundColor = [UIColor blueColor];
    [self.scrolV addSubview:self.blueView];
    
    self.scrolV.contentSize = CGSizeMake(0, CGRectGetMaxY(self.blueView.frame));
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offset = scrollView.contentOffset.y;
    NSLog(@"%f",offset);
    if(offset >= imgH){//往上移动，正好移动了图片高度时，将黄色toolBar悬停,悬停：实际上是将toolBar父视图变成view
        CGRect toolBarF = self.toolBar.frame;
        toolBarF.origin.y = 0;
        self.toolBar.frame = toolBarF;
        [self.view addSubview:self.toolBar];
    }else{//往下移动,toolBar又跟个动，实际上是将toolBar父视图变成scrollView
        CGRect toolBarF = self.toolBar.frame;
        toolBarF.origin.y = imgH;
        self.toolBar.frame = toolBarF;
        [self.scrolV addSubview:self.toolBar];
    }
    //由于下拉offset为-值，所以要用-号
    CGFloat scale = 1 - offset / 70;
    scale = (scale >= 1) ? scale : 1;
    //下拉图片放大
    self.headerImageView.transform = CGAffineTransformMakeScale(scale, scale);
}

@end
